<?php
session_start();
include_once('_class/DataBaseConnector.php');
$msg = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $db = new DataBaseConnector();
    $db->connect();
    $username = addslashes($_POST['username']);
    $password = addslashes($_POST['password']);

    $result = $db->selectDataWhere("users", "email, name, lastname", "email='$username' and password='$password'");
    if ($result == NULL) {
        $msg = "<P><font color=\"#FF0000\"><strong>Bad Login - Try Again</strong></font></P>";
    } else {
        $count = mysql_num_rows($result);

        if ($count == 1) {
            $_SESSION['logged_user'] = $username;
            $_SESSION['valid'] = true;
            header("location: index.php");
        }
    }
}


$form_block = "<h1>Login</h1>
    <form method=POST action=\"$_SERVER[PHP_SELF]\">
    $msg
    <table>
    <tr>
        <td><label>Email:</label></td>
        <td><input type=\"email\" name=\"username\" size=15 maxlength=25 class=\"large text\"/></td>
    </tr>
    
    <tr>
        <td><label>Password:</label></td>
        <td><input type=\"password\" name=\"password\" size=15 maxlength=25 class=\"large text\"/>   </td>
    </tr>
    </table>
    <input type=\"submit\" name=\"submit\" class=\"medium blue button\" value=\"Login\">
    <a href=\"index.php\" class=\"medium blue link\">Cancel</a>
    </form>";
?>
<html>
    <head>
        <title>Login</title>
        <link rel="stylesheet" href="resources/styles.css" type="text/css" media="screen" />
    </head>
    <body>
        <div id="wrapper">
            <header></header>
            <div id="page">
                <div id="content">
                    <?php echo "$form_block"; ?>
                </div>
                <aside>
                </aside>
            </div>
            <footer>
                <section>
                    <p>Copyright ©2012 - All Rights Reserved</p>
                </section>
            </footer>
        </div>
    </body>
</html>